<template>
  <div class="chooseStaffBody">
    <div class="staffBox">
      <div class="staffLf">
        <CustomerList
          :staffInfoList="chooseStaffBodyData.staffInfoList"
          :checkedStaffList="chooseStaffBodyData.checkedStaffList"
          @checkStaff="checkStaff"
          @checkAllStaff="checkAllStaff"
          :filter="filter"
        />
      </div>
      <div class="staffRg">
        <CheckedCustomerList :checkedStaffList="chooseStaffBodyData.checkedStaffList" @deleteStaff="deleteStaff" />
      </div>
    </div>
    <p class="chooseStaffBtnBox">
      <span class="cancelBtn" @click="closeModal">取 消</span>
      <span class="affirmBtn" @click="affirm">确 定</span>
    </p>

    <ZdyLoading v-show="loading" />
  </div>
</template>

<script>
import { reactive,watch } from 'vue'
import CustomerList from './CustomerList'
import CheckedCustomerList from './CheckedCustomerList'
import ZdyLoading from '@/components/ZdyLoading'

export default {
  name: 'ChooseCustomerBody',
  components: {
    CustomerList,
    CheckedCustomerList,
    ZdyLoading
  },
  props: {
    staffInfoList: {
      //成员列表
      type: Array,
      default: []
    },
    checkedStaffList: {
      //已选中的成员列表
      type: Array,
      default: []
    },
    filter: {
      //搜索是否使用过滤（用全选功能不要使用，会有问题）
      type: Boolean,
      default: false
    },
    loading: {
      type: Boolean,
      default: false
    }
  },
  setup(props, context) {
    const chooseStaffBodyData = reactive({
      staffInfoList: [], //成员列表
      checkedStaffList: [] //已选中的成员列表
    })

    //关闭弹窗
    const closeModal = () => {
      context.emit('closeModal', {
        affirm: false //是否确认选项
      })
    }

    //确认
    const affirm = () => {
      context.emit('closeModal', {
        affirm: true, //是否确认选项
        checkedStaffList: chooseStaffBodyData.checkedStaffList //选中的人员列表
      })
    }

    //选中/取消选中 单个成员
    const checkStaff = res => {
      //如果当前是选中，则在选中的成员列表中增加该成员；如果是取消选中，则再选中的成员列表中移除该成员
      console.log('已选用工单位', res)
      if (res.checked) {
        chooseStaffBodyData.checkedStaffList.push(res.staffInfo)
      } else {
        chooseStaffBodyData.checkedStaffList = chooseStaffBodyData.checkedStaffList.filter(item => {
          if (item.key !== res.staffInfo.key) {
            return item
          }
        })
      }
    }

    //全选/反选所有成员成员
    const checkAllStaff = res => {
      chooseStaffBodyData.checkedStaffList = res
    }

    //移除已选中的某个人员
    const deleteStaff = val => {
      chooseStaffBodyData.checkedStaffList = chooseStaffBodyData.checkedStaffList.filter(item => {
        if (item.key !== val) {
          return item
        }
      })
    }

    //监听成员列表
    watch(
      () => props.staffInfoList,
      list => {
        chooseStaffBodyData.staffInfoList = list
      },
      {
        immediate: true
      }
    )

    watch(
      () => props.checkedStaffList,
      list => {
        chooseStaffBodyData.checkedStaffList = list
      },
      {
        immediate: true
      }
    )

    return {
      chooseStaffBodyData,
      checkStaff,
      checkAllStaff,
      deleteStaff,
      closeModal,
      affirm
    }
  }
}
</script>

<style lang="less" scoped>
.chooseStaffBody {
  position: relative;
  .staffBox {
    display: flex;
    height: 451px;
    .staffLf,
    .staffRg {
      flex: none;
      width: 50%;
      height: 100%;
    }
    .staffLf {
      position: relative;
      &::before {
        position: absolute;
        content: '';
        top: 0px;
        right: 0px;
        height: 100%;
        width: 1px;
        background: #efefef;
      }
    }
  }
  .chooseStaffBtnBox {
    padding: 30px 32px 0px;
    text-align: right;
    margin: 0px;
    span {
      display: inline-block;
      height: 36px;
      line-height: 34px;
      padding: 0px 16px;
      border-radius: 4px;
      font-size: 14px;
      margin-left: 8px;
      cursor: pointer;
      user-select: none;
      &:hover {
        opacity: 0.7;
      }
      &:active {
        opacity: 0.95;
      }
      &.cancelBtn {
        color: rgba(9, 0, 0, 0.65);
        background: #ffffff;
        border: #d9d9d9 solid 1px;
      }
      &.affirmBtn {
        color: #ffffff;
        background: #5975fb;
        border: #5975fb solid 1px;
      }
    }
  }
}
</style>
